package com.huanxi.joytravel.bases;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;

import com.cloud.core.ObjectJudge;
import com.huanxi.joytravel.R;
import com.huanxi.joytravel.utils.ConvertUtils;
import com.huanxi.joytravel.utils.NetWatchdogUtils;
import com.huanxi.joytravel.utils.ToastUtil;
import com.huanxi.joytravel.widgets.LoadingDialog;
import com.huanxi.joytravel.widgets.ProgressLinearLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment extends Fragment {

    protected View rootView;
    protected Context mContext = null;
    private Unbinder unbinder;
    protected String TAG = null;
    private boolean mHaveLoadData;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        this.mContext = inflater.getContext();
        if (!NetWatchdogUtils.hasNet(getContext())) {
            showShortToast("网络未连接");
        }
        init(rootView);
        return rootView;
    }
    public void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getActivity().getWindow();
            View decorView = window.getDecorView();
            decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                    return defaultInsets.replaceSystemWindowInsets(
                            defaultInsets.getSystemWindowInsetLeft(),
                            0,
                            defaultInsets.getSystemWindowInsetRight(),
                            defaultInsets.getSystemWindowInsetBottom());
                }
            });
            ViewCompat.requestApplyInsets(decorView);
            //将状态栏设成透明，如不想透明可设置其他颜色
            window.setStatusBarColor(ContextCompat.getColor(mContext, android.R.color.transparent));
        }
    }
    /**
     * 子类实现
     */
    //获取布局文件
    protected abstract int getLayoutId();

    protected abstract void init(View view);


    @Override
    public void onDestroy() {
        super.onDestroy();
        //避免mvp的内存泄漏
        unbinder.unbind();
    }

    /**
     * 开启加载进度条
     */
    public void startProgressDialog() {
        LoadingDialog.showDialogForLoading(getActivity());
    }

    /**
     * 开启加载进度条
     *
     * @param msg
     */
    public void startProgressDialog(String msg) {
        LoadingDialog.showDialogForLoadingDialog(getActivity(), msg, true);
    }

    /**
     * 停止加载进度条
     */
    public void stopProgressDialog() {
        LoadingDialog.cancelDialogForLoading();
    }

    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUtil.showNorToast(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUtil.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUtil.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUtil.showLong(text);
    }


    public void showToastWithImg(String text, int res) {
        //ToastUtil.showToastWithImg(text, res);
    }

    /**
     * 带图片的toast
     *
     * @param text 操作成功提示
     */
    public void showToastWithImgAndSuc(String text) {
        //ToastUtil.showToastWithImgAndSuc(text);
    }

    public void showError(ProgressLinearLayout progress, String errorTitle) {
        if (NetWatchdogUtils.hasNet(getActivity())) {
            progress.showError(R.mipmap.ic_empty_defalt, errorTitle, "", "点击重试", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    init(rootView);
                }
            });
        } else {
            progress.showError(R.mipmap.ic_zw_no_net, getNetErrStrirng(), "", "点击重试", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    EventBusUtils.post(new MessageEvent(NumConfig.LOGIN_SUCCESS));
                    init(rootView);
                }
            });
        }
    }

    protected void unLoginOpenLoginActivity() {
//        startActivity(LoginActivity.class);

    }

    public String getNetErrStrirng() {
        return mContext.getResources().getString(R.string.net_err);
    }

    public String getEmptyStrirng() {
        return mContext.getResources().getString(R.string.request_empty);
    }

    public String getRequestErrStrirng() {
        return mContext.getResources().getString(R.string.request_err);
    }

    /**
     * 从bundle中获取字符串
     *
     * @param bundle       bundle对象
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public String getStringBundle(Bundle bundle, String key, String defaultValue) {
        if (bundle != null && bundle.containsKey(key)) {
            Object o = bundle.get(key);
            if (o == null) {
                return defaultValue;
            }
            return String.valueOf(o);
        } else {
            return defaultValue;
        }
    }

    /**
     * 从bundle中获取字符串
     *
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public String getStringBundle(String key, String defaultValue) {
        Bundle bundle = getBundle();
        return getStringBundle(bundle, key, defaultValue);
    }
    public Bundle getBundle() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return new Bundle();
        } else {
            return bundle;
        }
    }
    /**
     * 从bundle中获取字符串
     *
     * @param key
     * @return
     */
    public String getStringBundle(String key) {
        return getStringBundle(key, "");
    }
    /**
     * 从bundle中获取boolean值
     *
     * @param bundle       bundle对象
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public boolean getBooleanBundle(Bundle bundle, String key, boolean defaultValue) {
        if (bundle != null && bundle.containsKey(key)) {
            Object value = bundle.get(key);
            if (value == null) {
                return defaultValue;
            }
            return ObjectJudge.isTrue(value);
        } else {
            return defaultValue;
        }
    }

    /**
     * 从bundle中获取boolean值
     *
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public boolean getBooleanBundle(String key, boolean defaultValue) {
        Bundle bundle = getBundle();
        return getBooleanBundle(bundle, key, defaultValue);
    }

    /**
     * 从bundle中获取boolean值
     *
     * @param key
     * @return
     */
    public boolean getBooleanBundle(String key) {
        return getBooleanBundle(key, false);
    }
    /**
     * 从bundle中获取int值
     *
     * @param bundle       bundle对象
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public int getIntBundle(Bundle bundle, String key, int defaultValue) {
        if (bundle != null && bundle.containsKey(key)) {
            Object value = bundle.get(key);
            if (value == null) {
                return defaultValue;
            }
            return ConvertUtils.toInt(value, defaultValue);
        } else {
            return defaultValue;
        }
    }

    /**
     * 从bundle中获取int值
     *
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public int getIntBundle(String key, int defaultValue) {
        Bundle bundle = getBundle();
        return getIntBundle(bundle, key, defaultValue);
    }

    /**
     * 从bundle中获取int值
     *
     * @param key
     * @return
     */
    public int getIntBundle(String key) {
        return getIntBundle(key, 0);
    }

}
